<template>
    <div class="list" ref='wrap'>
        <div class="content">
            <div class="area">
                <div class="title border-topbottom">当前城市</div>
                <div class="button-list">
                    <div class="bottn-wraper">
                        <div class="button">{{this.city}}</div>
                    </div>
                </div>
            </div>
            <div class="area">
                <div class="title border-topbottom">热门城市</div>
                <div class="button-list" >
                    <div class="bottn-wraper" 
                        v-for="item of hotCities" 
                        :key='item.id'
                        @click="handleClick(item.name)"
                    >
                        <div class="button">{{item.name}}</div>
                    </div>
                </div>
            </div>
            <div class="area" 
                    v-for="(items,key) of cities"
                    :key="key"
                    :ref="key">
                <div class="title border-topbottom" >{{key}}</div>
                
                    <div class="item-list" 
                        v-for="item of items" 
                        :key='item.id'
                        @click="handleClick(item.name)"
                    >
                        <div class="item border-bottom">{{item.name}}</div>             
                    </div>
               
                
            </div>
        </div>
    </div>
</template>
<script>
import Bscroll from 'better-scroll'
import { mapState,mapMutations  } from 'vuex'

export default {
    name:'cityList',
    methods: {
        handleClick(city){
            this.changeCity(city)
            this.$router.push('./')
        },
          ...mapMutations([
            'changeCity', // 将 `this.changeCity()` 映射为 `this.$store.commit('increment')`
        ]),
    },
    computed: mapState([
        // 映射 this.count 为 store.state.count
        'city'
    ]),
    mounted() {
        //创建scroll 实例
        this.scroll = new Bscroll(this.$refs.wrap)
    },
    props:{
        hotCities:Array,
        cities:Object,
        cityFname:String
    },
    watch: {
        //监听city传递的cityFname 根据改变scroll要显示的元素
        cityFname(){
            if(this.cityFname){
                // console.log(this.$refs[this.cityFname][0])
                let element=this.$refs[this.cityFname][0]
                //scrollToElement()方法 显示
                 this.scroll.scrollToElement(element)
            }   
        }
    },
}
</script>
<style lang="stylus" scoped>
    .border-topbottom
        &:before
            border-color:#ccc
        &:after
            border-color:#ccc
    .border-bottom
        &:before
            border-color:#ccc
    .title
        line-height :.44rem
        background :#eee
        padding-left :.2rem
        color:#666
        font-size :.26rem
    .list
        position: absolute
        top: 1.58rem
        left :0
        right:0
        bottom:0
        overflow: hidden
        .button-list
            padding: .1rem .6rem .1rem .1rem
            overflow :hidden
            .bottn-wraper
                width :33.33%
                float: left
                .button
                    margin :.1rem 
                    padding  :0.1rem 0
                    text-align :center
                    border :.02rem solid #ccc
                    border-radius :.06rem
        .item-list
            .item
                line-height :.76rem
                padding-left :.2rem
                color:#666
</style>